#!/usr/bin/python
# -*- coding: utf-8 -*-

import glob
import os
import re
import string
import sys
import time
import shutil
import platform

import glib as g

def u(a='', b='', c=''):
    ''' '''
    print '\n    %s %s %s' % (a, b, c),

    if a in ('replace-inhexadecimal-by-tab'):
        # THIS IS NOT FINISHED YET !!!
        # note, enter search string characters to be replaced in hexa form !!!
        of = open(b + '_', 'w')
        for line in g.bt.lines(b):
            for r in c:
                line = re.sub(r, '\t', line)
            of.write(line)
        of.close()

    # joint two small datasets via first column, using sqlite
    if a in ('join_dat_sqlite', 'join-dat-sqlite', 'join-two-tables', 'join_two_tables'):
        print '''\n    joining files per first column (key) in sqlite'''
        from pepe.p_utils.join_dat_sqlite import main
        main(b, c)

    # joint big datasets via first column, using postgresql
    if a in ('join_dat_pg', 'join-dat-pg', 'join_big-tables', 'join_big_tables'):
        print '''\n    joining files per first column (key) in pg'''
        from pepe.p_utils.join_dat_pg import main
        main(fMask=b, cDelimiter=c)

    if a in ('copy-files', 'copy_files'):
        print '''\n    copy files'''
        if len(glob.glob(b)) == 1:
            shutil.copy(b, c)
        else:
            for f in glob.glob(b):
                shutil.copy(f, c)

    if a in ('count-delimiters', 'count_delimiters', 'csv_cntdel'):
        from pepe.p_utils.csv_cntdel import cntdel as cntd
        cntd(inpfilemask=b)

    if a in ('count-header-tail', 'count_header_tail', 'cht'):
        print '''\n    count rows, show header and tail sample (cht#)'''
        from pepe.p_utils.cht import main as cht_main
        cht_main(b)

    if a in ('data-add-unique-id', 'data_add_unique_id'):
        from pepe.p_utils.data_add_unique_id import main as daui_main
        daui_main(b)

    if a in ('data-csv-header', 'data_csv_header', 'csvh'):
        print '''\n    csv header info file (csvh#)'''
        from pepe.p_utils.csvh import main as csvh_main
        csvh_main(b)

    if a in ('data-csv-strip', 'data_csv_strip', 'csv-strip', 'csv_strip'):
        # striping blank spaces around columns delimiters
        import pepe.p_utils.csv_strip as csvs
        csvs.main(b)

    if a in ('data-import-to-db', 'data_import_to_db', 'import-data-pg', 'pgimport'):
        from pepe.p_sqlpg.pg_import import main as dldb_main
        dldb_main(b)

    if a in ('data-rename-abc', 'data_rename_abc'):
        from pepe.p_utils.data_rename_abc import main_aspylib as drab_main
        if c == '':
            drab_main(filemask=b)
        elif c <> '':
            drab_main(filemask=b, start=c)

    if a in ('data-replace-delimiter-by-tab', 'data_replace_delimiter_by_tab'):
        from pepe.p_utils.data_replace_delimiter_by_tab import main as drdt_main
        drdt_main(b)

    if a in ('export-pgtable', 'export_pgtable', 'epg', 'pgexport'):
        from pepe.p_sqlpg.pg_export import main as expg_main
        if c == '':
            expg_main(db='work', tblmask=b)
        elif c <> '':
            expg_main(db=b, tblmask=c)

    if a in ('file-name-append', 'file_name_append'):
        print '''\n    name append'''
        if len(glob.glob(b)) == 1:
            shutil.move(b, "%s%s" % (b, c))
        else:
            for f in glob.glob(b):
                shutil.move(f, "%s%s" % (f, c))

    if a in ('move-files', 'move_files', 'rename-files', 'rename_files'):
        print '''\n    move/rename files'''
        if len(glob.glob(b)) == 1:
            shutil.move(b, c)
        else:
            for f in glob.glob(b):
                shutil.move(f, c)

    if a in ('run-sql-file', 'run-sql-file'):
        from pepe.p_utils.pgrun import main as pgrun_main
        print '\n    ', g.timenow()
        pgrun_main(b)
        print '    ', g.timenow()

    if a in ('work-db-create-setup', 'work_db_create_setup'):
        print '''\n    create and setup pg work db'''
        from pepe.p_utils.pgrun import main as pgrun_main
        print '\n    ', timenow()

        if os.name == 'posix':
            os.system('psql -h 127.0.0.1 -d postgres -U postgres -f /usr/local/lib/python2.6/dist-packages/pepe/p_sql/_create-work-db.sql')
            pgrun_main('/usr/local/lib/python2.6/dist-packages/pepe/p_sql/_plpgsql.sql')
            pgrun_main('/usr/local/lib/python2.6/dist-packages/pepe/p_sql/_plpythonu')
            pgrun_main('/usr/local/lib/python2.6/dist-packages/pepe/p_sql/_create-work-db-schemas.sql')
            pgrun_main('/usr/local/lib/python2.6/dist-packages/pepe/p_sql/_setup-work-db.sql')
        if os.name == 'nt':
            os.system('psql -h 127.0.0.1 -d postgres -U postgres -f c:/aft/pepe/p_sql/_create-work-db.sql')
            pgrun_main('c:/aft/pepe/p_sql/_create-work-db-schemas.sql')
            pgrun_main('c:/aft/pepe/p_sql/_setup-work-db.sql')

        print '    ', timenow()

if __name__ == "__main__":
    ''' '''
    import doctest
    doctest.testmod()
